package org.rbreznak.tronco.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySQLConnection implements DBConnectionInterface {

	private String hostname;
	private String username;
	private String password;
	private String database;
	private Connection conn;
	
	public MySQLConnection(String host, String username, String password, String database) {
		setHost(host);
		setUsername(username);
		setPassword(password);
		setDatabase(database);
	}
	
	public String getDatabase() {
		return database;
	}

	public String getHost() {
		return hostname;
	}

	public String getPassword() {
		return password;
	}

	public String getUsername() {
		return username;
	}

	public void setDatabase(String database) {
		this.database = database;
	}

	public void setHost(String hostname) {
		this.hostname = hostname;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Connection getConnection() {
        return conn;
	}

	public void makeConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection("jdbc:mysql://" + getHost() + "/" + database + "?user=" + getUsername() + "&password=" + getPassword());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Connection: " + conn);
	}
	
	public void close() {
		try { 
			conn.close();
		}catch( Exception e ) {
			e.printStackTrace();
		}
	}
}
